﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;

namespace AdminUI.Pages.SysManage
{
    public partial class SysUserModel : BaseModel
    {
        public Dictionary<int, string> Roles { get; set; }

        public void OnGet()
        {
            var req = new PagingRequest
            {
                PageIndex = 1,
                PageSize = 20
            };
            var result = SysUserBO.Instance.Query(req, OnlineTID);
            ViewBag.DataSource = result.Data;
            ViewBag.Total = result.Total;
            ViewBag.PageSize = result.PageSize;

            Roles = RoleBO.Instance.GetDic();
        }

        public IActionResult OnPostGrid_DoAction(QueryData<long> data, string condition, string keyword)
        {
            var grid = UIHelper.Grid("Grid1");
            var req = new PagingRequest
            {
                PageIndex = data.PageIndex + 1, //注意：数据表格组件分页索引从0开始
                PageSize = data.ChangedPageSize,
                SortExp = data.SortExp,
            };
            if (!string.IsNullOrEmpty(keyword) && !string.IsNullOrEmpty(condition))
            {
                req.FilterItems.Add(new FilterItem { Field = condition, Value = keyword });
            }
            if (data.IsChangedPageSize)
            {
                grid.PageSize(data.ChangedPageSize);
                grid.PageIndex(0); //改变PageSize大小后重置分页索引，表格控件页索引从0开始
                req.PageIndex = 1;
            }

            try
            {
                switch (data.ActionType)
                {
                    case "delete":
                        SysUserBO.Instance.Delete(data.SelectedID);
                        LogAction(ActionType.删除, $"删除用户：{data.SelectedID}");
                        break;
                    case "enable":
                        SysUserBO.Instance.SetEnable(data.SelectedID, true);
                        LogAction(ActionType.启用, $"启用用户：{data.SelectedID}");
                        break;
                    case "disable":
                        SysUserBO.Instance.SetEnable(data.SelectedID, false);
                        LogAction(ActionType.禁用, $"禁用用户：{data.SelectedID}");
                        break;
                    default:
                        break;
                }

                var result = SysUserBO.Instance.Query(req, OnlineTID);
                grid.RecordCount(result.Total);
                grid.DataSource(result.Data, data.Fields);
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }
    }
}
